package com.rerun.phone.memberuser;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rerun.common.anno.ResponseExceptionReturn;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.entity.ChatRecords;
import com.rerun.enums.ChatRecordsStatus;
import com.rerun.service.IChatRecordsService;
import com.rerun.vo.memberUser.ChatRecordsSearchVo;
import com.rerun.vo.memberUser.ChatRecordsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * The type Chat record controller.
 */
@Api(value = "求职端-聊天记录", description = "求职端-聊天记录")
@RestController
@RequestMapping("/memberApi/chat")
public class ChatRecordController extends SuperController{
	/**
	 * The Chat records service.
	 */
	@Autowired
	IChatRecordsService chatRecordsService;

	/**
	 * Add rest.
	 *
	 * @param records the records
	 * @return rest
	 */
	@ApiOperation(value = "添加聊天记录", httpMethod = "POST",notes = "添加聊天记录")
	@RequestMapping(value = "/add",method=RequestMethod.POST)
	@ApiImplicitParams({
		 @ApiImplicitParam(name = "memberId",required = true, value = "用户ID",paramType ="query"),
		 @ApiImplicitParam(name = "jobId",required = false, value = "职位ID",paramType ="query"),
		 @ApiImplicitParam(name = "parkId",required = true, value = "园ID",paramType ="query")
	})
	@ResponseExceptionReturn
	public Rest add(@ModelAttribute ChatRecords records) {
		try {
			records.setUserStatus(ChatRecordsStatus.IS_SHOW.getKey());
			records.setParkStatus(ChatRecordsStatus.IS_SHOW.getKey());
			records.setLastTime(new Date());
			records.setCreateTime(new Date());
			chatRecordsService.addChatRecords(records,true);
			return Rest.ok("添加成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("添加失败");
	}

	/**
	 * List rest.
	 *
	 * @param memberId   the member id
	 * @param pageNumber the page number
	 * @return the rest
	 */
	@ApiOperation(value = "聊天列表", httpMethod = "GET",notes = "聊天记录列表")
	@RequestMapping(value = "/list/{memberId}/{pageNumber}",method=RequestMethod.GET)
	@ResponseExceptionReturn
	public Rest list(@PathVariable String memberId,@PathVariable Integer pageNumber) {
		try {
			Page<ChatRecordsVo> page = getPage(pageNumber);
			ChatRecordsSearchVo chatRecords = new ChatRecordsSearchVo();
			chatRecords.setMemberId(memberId);
			chatRecords.setUserStatus(ChatRecordsStatus.IS_SHOW.getKey());
			page = chatRecordsService.selectRecordsList(chatRecords,page);
			return Rest.okData(page.getRecords());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("获取列表失败");
	}

	/**
	 * Delete rest.
	 *
	 * @param chatId the chat id
	 * @return the rest
	 */
	@ApiOperation(value = "删除聊天记录", httpMethod = "GET",notes = "删除聊天记录")
	@RequestMapping(value = "/delete/{chatId}",method=RequestMethod.GET)
	@ResponseExceptionReturn
	public Rest delete(@PathVariable String chatId) {
		try {
			ChatRecords records = new ChatRecords();
			records.setUserStatus(ChatRecordsStatus.IS_DELETE.getKey());
			EntityWrapper<ChatRecords> wrapper = new EntityWrapper<>();
			wrapper.eq("chatId", chatId);
			chatRecordsService.update(records, wrapper);
			return Rest.ok("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Rest.failure("删除失败");
	}
}
